package service.impl;

import java.util.Date;
import java.util.List;

import persistence.dao.TdfkfaBulletinDAO;
import persistence.dao.TdfkfaFileDAO;
import persistence.model.TdfkfaBulletin;
import persistence.model.TdfkfaBulletinExample;
import persistence.model.TdfkfaFile;
import presentation.extendmodel.BulletinDetailModel;
import presentation.queryvo.BulletinQueryVO;
import service.BulletinService;

public class BulletinServiceImpl implements BulletinService {

    private TdfkfaBulletinDAO tdfkfaBulletinDAO;

    private TdfkfaFileDAO tdfkfaFileDAO;

    public List<TdfkfaBulletin> getBulletinList(BulletinQueryVO queryVO, int pageIndex, int pageSize) {

        TdfkfaBulletinExample example = new TdfkfaBulletinExample();
        example.setOrderByClause("create_date desc");
        List<TdfkfaBulletin> list = tdfkfaBulletinDAO.selectBulletinListByExample(example, pageIndex, pageSize);
        return list;
    }
    
    public int countBulletin(BulletinQueryVO queryVO) {
        
        TdfkfaBulletinExample example = new TdfkfaBulletinExample();
        return tdfkfaBulletinDAO.countByExample(example);
    }

    public BulletinDetailModel getBulletinDetail(Integer bulletinId) {

        BulletinDetailModel bulletinDetail = new BulletinDetailModel();
        TdfkfaBulletin bulletin = tdfkfaBulletinDAO.selectByPrimaryKey(bulletinId);
        bulletinDetail.setBulletin(bulletin);

        List<TdfkfaFile> fileList = tdfkfaFileDAO.selectByBulletinId(bulletinId);
        bulletinDetail.setAttachmentFileList(fileList);

        return bulletinDetail;
    }

    public void saveBulletin(String title, String content) {

        TdfkfaBulletin record = new TdfkfaBulletin();

        record.setTitle(title);
        record.setContent(content);
        Date now = new Date();
        record.setCreateDate(now);
        record.setUpdateDate(now);

        tdfkfaBulletinDAO.insertSelective(record);
    }

    public void deleteBulletin(Integer[] bulletinId) {
        for (Integer bid : bulletinId) {
            tdfkfaBulletinDAO.deleteByPrimaryKey(bid);
        }
    }
    
    public void updateBulletin(String bulletinId, String bulletinTitle, String bulletinContent) {
        TdfkfaBulletin tdfkfaBulletin = new TdfkfaBulletin();
        tdfkfaBulletin.setBid(new Integer(bulletinId));
        tdfkfaBulletin.setTitle(bulletinTitle);
        tdfkfaBulletin.setContent(bulletinContent);
        tdfkfaBulletin.setUpdateDate(new Date());
        tdfkfaBulletinDAO.updateByPrimaryKeySelective(tdfkfaBulletin);
    }

    public TdfkfaBulletin getBulletin(Integer bulletinId) {
        return tdfkfaBulletinDAO.selectByPrimaryKey(bulletinId);
    }

    public void setTdfkfaBulletinDAO(TdfkfaBulletinDAO tdfkfaBulletinDAO) {
        this.tdfkfaBulletinDAO = tdfkfaBulletinDAO;
    }

    public void setTdfkfaFileDAO(TdfkfaFileDAO tdfkfaFileDAO) {
        this.tdfkfaFileDAO = tdfkfaFileDAO;
    }

}
